﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Persistence;
using Persistence.Dao;
using System.Text.RegularExpressions;
namespace web
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["admin"] == null)
                {
                    Response.Write("只有管理员才可以添加销售商账户,请登陆后台管理再试");
                    Response.End();
                }
            }
        }

        protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
        {
            if (agree.Checked)
            {
                if (txtPassword1.Value.Trim() == txtPassword2.Value.Trim())
                {
                    Domain.User u = new Domain.User();
                    u.addressCorp = txtAddress.Value.Trim();
                    u.email = txtEmail.Value.Trim();
                    u.mobile = txtMobile.Value.Trim();
                    u.password = txtPassword1.Value.Trim();
                    u.postCode = Common.Utils.StrToInt(txtPostCode.Value.Trim(), 0);
                    u.regDate = DateTime.Now;
                    u.telephone = txtTel.Value.Trim();
                    u.trueName = txtTrueName.Value.Trim();
                    u.userName = txtUserName.Value.Trim();
                    if (Regex.IsMatch(txtUserCode.Value.Trim(), "^[a-zA-Z]{2}$",RegexOptions.IgnoreCase))
                    {
                        u.userCode = txtUserCode.Value.Trim().ToUpper();
                    }
                    else
                    {
                        Common.Message.alert("经销商编码必须是两位英文字符");
                        return;
                    }
                    if (u.userName.Length == 0)
                    {
                        Common.Message.alert("请输入用户名");
                        return;
                    }
                    if (u.telephone.Length == 0 && u.mobile.Length == 0)
                    {
                        Common.Message.alert("联系电话和手机请至少输入一个");
                        return;
                    }
                    if (Singleton<UserDao>.Instance.SelectUserExists(u.userName) > 0)
                    {
                        Common.Message.alert("用户已存在");
                        return;
                    }
                    Singleton<UserDao>.Instance.Insert(u);
                    
                    Common.Message.alertAndRedirect("注册用户成功", "/default.aspx");
                }
                else
                {
                    Common.Message.alert("两次密码不一致，请重新输入");
                }

            }
        }
    }
}
